package com.cjx.api.service.inner;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectOne;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.cjx.api.common.ErrorCode;
import com.cjx.api.exception.BusinessException;
import com.cjx.api.model.dto.userinterfaceinvoke.UserInterfaceInvokeAddRequest;
import com.cjx.api.model.entity.User;
import com.cjx.api.model.entity.UserInterfaceInvoke;
import com.cjx.api.service.InterfaceInfoService;
import com.cjx.api.service.UserInterfaceInvokeService;
import com.cjx.api.service.UserService;
import com.cjx.common.model.entity.InterfaceInfo;
import com.cjx.common.service.inner.InterfaceInfoServiceInner;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

/**
 * @author cjx
 * @description
 * @createDate 2024/5/14 13:26
 **/
@DubboService
public class InterfaceInfoServiceInnerImpl implements InterfaceInfoServiceInner {

    @Resource
    private InterfaceInfoService interfaceInfoService;
    @Resource
    private UserService userService;
    @Resource
    private UserInterfaceInvokeService userInterfaceInvokeService;



    @Override
    public InterfaceInfo getInterfaceInfoByMethodAndUri(String method, String uri) {
        LambdaQueryWrapper<com.cjx.api.model.entity.InterfaceInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(com.cjx.api.model.entity.InterfaceInfo::getMethod,method)
                .like(com.cjx.api.model.entity.InterfaceInfo::getUrl,uri);
        com.cjx.api.model.entity.InterfaceInfo interfaceInfo = interfaceInfoService.getOne(queryWrapper);
        return BeanUtil.copyProperties(interfaceInfo, com.cjx.common.model.entity.InterfaceInfo.class);
    }

    @Override
    public boolean invoke(Long interfaceId, Long userId, Long reduceScore) {
        // 6. 调用次数和余额扣减
        LambdaUpdateWrapper<com.cjx.api.model.entity.InterfaceInfo> infoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        infoLambdaUpdateWrapper.eq(com.cjx.api.model.entity.InterfaceInfo::getId,interfaceId);
        infoLambdaUpdateWrapper.setSql("totalInvokes =  totalInvokes + 1");
        if (!interfaceInfoService.update(infoLambdaUpdateWrapper)) {
            return false;
        }
        // 6.1 判断是否存在此条调用关系
        UserInterfaceInvoke invokeRecording = userInterfaceInvokeService.getOne(
                new LambdaQueryWrapper<UserInterfaceInvoke>()
                        .eq(UserInterfaceInvoke::getUserId, userId)
                        .eq(UserInterfaceInvoke::getInterfaceId, interfaceId)
        );
        if (invokeRecording != null){
            //6.2 存在则修改
            boolean onceRes = userInterfaceInvokeService.invokeResultInUpdate(invokeRecording.getId());
            if (!onceRes){
                return false;
            }
        }else{
            //6.3 不存在则创建调用记录
            UserInterfaceInvokeAddRequest newInvokeRecording = new UserInterfaceInvokeAddRequest();
            newInvokeRecording.setUserId(userId);
            newInvokeRecording.setInterfaceId(interfaceId);
            newInvokeRecording.setTotalInvokes(1L);
            boolean onceRes = userInterfaceInvokeService.addUserInterfaceInvoke(newInvokeRecording);
            if (!onceRes){
                return false;
            }
        }
        //6.4 扣减用户余额
        if (!userService.updateUserBalance(userId,reduceScore)) {
            return false;
        }
        return true;
    }
}
